from abc import abstractmethod
from pathlib import Path
from typing import Iterable
from feasytools import TableWriter, createTableWriter
import numpy as np
from fplugins import *

def cross_list(a:Iterable[str],b:Iterable[str])->list[str]:
    '''生成交叉表头'''
    return [f"{i}#{j}" for j in b for i in a]

class StaBase:
    '''统计项记录器'''
    @abstractmethod
    def __init__(self,name:str,path:str,suffix:str,cols:list[str],tinst:TrafficInst,plugins:dict[str,PluginBase]):
        self._name=name
        self._inst=tinst
        self._plug=plugins
        self._columns=cols
        self._writer=createTableWriter(str(Path(path)/(self._name+suffix)),self._columns,np.float32)
    
    @abstractmethod
    def GetData(self,inst:TrafficInst,plugins:dict[str,PluginBase])->list: 
        '''获取数据'''
        raise NotImplementedError
    
    def LogOnce(self):
        data = self.GetData(self._inst,self._plug)
        assert len(data) == len(self._columns), f"{self._name}: 数据长度{len(data)}与表头长度{len(self._columns)}不一致"
        self._writer.write(data)

    @property
    def Writer(self)->TableWriter: return self._writer

    def close(self):
        self._writer.close()
